<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="姓名" prop="nickname">
        <el-input
          v-model="queryParams.nickname"
          placeholder="请输入教师姓名"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>

      <el-form-item label="性别" prop="sex">
        <el-select
          v-model="queryParams.sex"
          placeholder="请选择教师性别"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.SYSTEM_USER_SEX)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="帐号状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择帐号状态"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.COMMON_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="所属园区" prop="schoolCode">
        <el-select
          v-model="queryParams.schoolCode"
          placeholder="请选择所属园区"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.SCHOOL_CODE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="民族" prop="ethnicity">
        <el-select
          v-model="queryParams.ethnicity"
          placeholder="请选择民族"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.SCHOOL_TEACHER_ETHNICITY)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="学历" prop="education">
        <el-select
          v-model="queryParams.education"
          placeholder="请选择学历"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.SCHOOL_TEACHER_EDUCATION)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="技术职称" prop="technicalTitle">
        <el-select
          v-model="queryParams.technicalTitle"
          placeholder="请选择技术职称"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.SCHOOL_TEACHER_TECHNICAL_TITLE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="持证类别" prop="certificateType">
        <el-select
          v-model="queryParams.certificateType"
          placeholder="请选择持证类别"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.SCHOOL_TEACHER_CERTIFICATE_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="就职状态" prop="employmentStatus">
        <el-select
          v-model="queryParams.employmentStatus"
          placeholder="请选择就职状态"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.SCHOOL_TEACHER_EMPLOYMENT_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="政治面貌" prop="politicalStatus">
        <el-select
          v-model="queryParams.politicalStatus"
          placeholder="请选择政治面貌"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.SCHOOL_TEACHER_POLITICAL_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>

        <!-- <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['school:users:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button> -->

        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['school:users:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table
      v-loading="loading"
      :data="list"
      :stripe="true"
      :show-overflow-tooltip="true"
      @current-change="handleCurrentChange"
    >
      <!-- 子表的列表 内嵌模式-->
      <!-- <el-table-column type="expand">
        <template #default="scope">
          <el-tabs model-value="teacherCertificates">
            <el-tab-pane label="教育背景" name="teacherEducations">
              <TeacherEducationsList :teacher-id="scope.row.id" />
            </el-tab-pane>

            <el-tab-pane label="工作经历" name="teacherWorkList">
              <TeacherWorkListList :teacher-id="scope.row.id" />
            </el-tab-pane>

            <el-tab-pane label="工作调度" name="teacherJobList">
              <TeacherJobListList :teacher-id="scope.row.id" />
            </el-tab-pane>

            <el-tab-pane label="专业技术资格" name="teacherCertificates">
              <TeacherCertificatesList :teacher-id="scope.row.id" />
            </el-tab-pane>
            
            <el-tab-pane label="奖励情况" name="teacherRewards">
              <TeacherRewardsList :teacher-id="scope.row.id" />
            </el-tab-pane>
            
            <el-tab-pane label="惩处情况" name="teacherPunishments">
              <TeacherPunishmentsList :teacher-id="scope.row.id" />
            </el-tab-pane>
            
          </el-tabs>
        </template>
      </el-table-column> -->

      <!-- <el-table-column label="用户ID" align="center" prop="id" /> -->
      <el-table-column label="登录名" align="center" prop="username" width="100" />
      <!-- <el-table-column label="密码" align="center" prop="password" /> -->
      <el-table-column label="教师姓名" align="center" prop="nickname" />

      <!-- <el-table-column label="部门ID" align="center" prop="deptId" /> -->
      <!-- <el-table-column label="岗位编号数组" align="center" prop="postIds" /> -->
      <el-table-column label="手机号码" align="center" prop="mobile" width="120" />
      <el-table-column label="性别" align="center" prop="sex">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.SYSTEM_USER_SEX" :value="scope.row.sex" />
        </template>
      </el-table-column>
      <!-- <el-table-column label="头像地址" align="center" prop="avatar" /> -->

      <el-table-column label="所属园区" align="center" prop="schoolCode">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.SCHOOL_CODE" :value="scope.row.schoolCode" />
        </template>
      </el-table-column>
      <el-table-column
        label="出生日期"
        align="center"
        prop="birthDate"
        :formatter="dateFormatter2"
        width="100px"
      />
      <el-table-column label="民族" align="center" prop="ethnicity">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.SCHOOL_TEACHER_ETHNICITY" :value="scope.row.ethnicity" />
        </template>
      </el-table-column>
      <el-table-column label="学历" align="center" prop="education">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.SCHOOL_TEACHER_EDUCATION" :value="scope.row.education" />
        </template>
      </el-table-column>
      <el-table-column label="技术职称" align="center" prop="technicalTitle">
        <template #default="scope">
          <dict-tag
            :type="DICT_TYPE.SCHOOL_TEACHER_TECHNICAL_TITLE"
            :value="scope.row.technicalTitle"
          />
        </template>
      </el-table-column>
      <el-table-column label="持证类别" align="center" prop="certificateType">
        <template #default="scope">
          <dict-tag
            :type="DICT_TYPE.SCHOOL_TEACHER_CERTIFICATE_TYPE"
            :value="scope.row.certificateType"
          />
        </template>
      </el-table-column>
      <el-table-column label="就职状态" align="center" prop="employmentStatus">
        <template #default="scope">
          <dict-tag
            :type="DICT_TYPE.SCHOOL_TEACHER_EMPLOYMENT_STATUS"
            :value="scope.row.employmentStatus"
          />
        </template>
      </el-table-column>
      <el-table-column label="政治面貌" align="center" prop="politicalStatus">
        <template #default="scope">
          <dict-tag
            :type="DICT_TYPE.SCHOOL_TEACHER_POLITICAL_STATUS"
            :value="scope.row.politicalStatus"
          />
        </template>
      </el-table-column>
      <!-- <el-table-column label="家庭地址" align="center" prop="homeAddress" /> -->
      <!-- <el-table-column label="身份证号" align="center" prop="idNumber" /> -->
      <el-table-column
        label="入职时间"
        align="center"
        prop="hireDate"
        :formatter="dateFormatter2"
        width="100px"
      />

      <!-- <el-table-column
        label="离职时间"
        align="center"
        prop="leaveDate"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="离职原因" align="center" prop="leaveReason" />
      
      <el-table-column
        label="退休日期"
        align="center"
        prop="retirementDate"
        :formatter="dateFormatter2"
        width="180px"
      /> -->

      <el-table-column label="帐号状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.COMMON_STATUS" :value="scope.row.status" />
        </template>
      </el-table-column>
      <!-- <el-table-column label="审批状态" align="center" prop="bpmStatus">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.BPM_PROCESS_INSTANCE_STATUS" :value="scope.row.bpmStatus" />
        </template>
      </el-table-column> -->
      <el-table-column label="邮箱" align="center" prop="email" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="最后登录IP" align="center" prop="loginIp" width="160" />
      <el-table-column
        label="最后登录时间"
        align="center"
        prop="loginDate"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />

      <el-table-column label="操作" align="center" fixed="right" width="100px">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['school:users:update']"
          >
            编辑
          </el-button>

          <!-- <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['school:users:delete']"
          >
            删除
          </el-button> -->
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <!-- <UsersForm ref="formRef" @success="getList" /> -->
  <TeacherDialog ref="teacherDialogRef" @success="getList" />
</template>

<script setup lang="ts">
import { getIntDictOptions, getStrDictOptions, DICT_TYPE } from '@/utils/dict'
import { dateFormatter, dateFormatter2 } from '@/utils/formatTime'
import download from '@/utils/download'
import { UsersApi, UsersVO } from '@/api/school/teacher'
import TeacherDialog from './teacherDialog.vue'

/** 教师信息 列表 */
defineOptions({ name: 'SchoolUsers' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<UsersVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  nickname: undefined,
  deptId: undefined,
  postIds: undefined,
  sex: undefined,
  status: undefined,
  createTime: [],
  schoolCode: undefined,
  ethnicity: undefined,
  education: undefined,
  technicalTitle: undefined,
  certificateType: undefined,
  employmentStatus: undefined,
  politicalStatus: undefined
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await UsersApi.getUsersPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const teacherDialogRef = ref()
const openForm = (type: string, id?: number) => {
  teacherDialogRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await UsersApi.deleteUsers(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await UsersApi.exportUsers(queryParams)
    download.excel(data, '教师信息.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 选中行操作 */
const currentRow = ref({}) // 选中行
const handleCurrentChange = (row) => {
  currentRow.value = row
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
